﻿Imports System.Data.SqlClient
Public Class frmGenerarReporte
    Private Sub btnRecuperarDatos_Click(sender As Object, e As EventArgs) Handles btnRecuperarDatos.Click
        Try
            If cboFiltro.SelectedIndex = 0 Then
                Me.SP_GenerarDatosReporteTableAdapter.Fill(Me.MEGASTOREDataSet.SP_GenerarDatosReporte, Me.dtpFechaInicio.EditValue, Me.dtpFechaFin.EditValue)
            ElseIf cboFiltro.SelectedIndex = 1 Then
                Me.SP_GenerarDatosReporteTableAdapter.FillBy(Me.MEGASTOREDataSet.SP_GenerarDatosReporte, Me.dtpFechaInicio.EditValue, Me.dtpFechaFin.EditValue)
            Else
                Me.SP_GenerarDatosReporteTableAdapter.FillBy1(Me.MEGASTOREDataSet.SP_GenerarDatosReporte, Me.dtpFechaInicio.EditValue, Me.dtpFechaFin.EditValue)
            End If


            GridView1.RefreshData()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnImprimirHojas_Click(sender As Object, e As EventArgs)
        Dim ret As String
        ' Le pasa la leyenda del cuadro de iálogo y el path inicial  
        ret = Buscar_Carpeta(" ... Seleccione una carpeta ")

        Me.dgvDatos.ExportToXlsx(ret + "\reporteGarantias.xlsx")
    End Sub

    Function Buscar_Carpeta(ByVal Titulo As String) As String

        Dim objShell As Object
        Dim objFolder As Object
        Dim o_Carpeta As Object

        ' Nuevo objeto Shell.Application  
        objShell = CreateObject("Shell.Application")

        On Error Resume Next
        'Abre el cuadro de diálogo para seleccionar  
        objFolder = objShell.BrowseForFolder( _
                                0, _
                                Titulo, _
                                0)

        ' Devuelve solo el nombre de carpeta  
        o_Carpeta = objFolder.Self

        ' Devuelve la ruta completa seleccionada en el diálogo  
        Buscar_Carpeta = o_Carpeta.Path

        Exit Function

    End Function

    Private Sub frmGenerarReporte_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.cboFiltro.Items.Add("Todos")
        Me.cboFiltro.Items.Add("Laptops")
        Me.cboFiltro.Items.Add("PCs de Escritorio")

        Me.cboFiltro.SelectedIndex = 0
        Me.dtpFechaFin.EditValue = Date.Today
        Me.dtpFechaInicio.DateTime = String.Format("1/{0}/{1}", Date.Today.Month, Date.Today.Year)
    End Sub

    Private Sub btnImprimirHojas_Click_1(sender As Object, e As EventArgs) Handles btnImprimirHojas.Click
        Dim ret As String
        ' Le pasa la leyenda del cuadro de iálogo y el path inicial  
        ret = Buscar_Carpeta(" ... Seleccione una carpeta ")

        Me.dgvDatos.ExportToXlsx(ret + "\reporteGarantias.xlsx")
    End Sub

    Private Sub EliminarFacturaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EliminarFacturaToolStripMenuItem.Click
        Dim resp As String
        resp = MessageBox.Show(String.Format("¿Está seguro de eliminar la factura {0}?", Me.GridView1.GetRowCellValue(Me.GridView1.FocusedRowHandle, "NumFactura")), varTitulo, MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        If resp = vbYes Then
            EliminarFacturaDeGarantia()
        End If
    End Sub

    Private Sub EliminarFacturaDeGarantia()
        Try
            If varConexionLocal.State = ConnectionState.Open Then
                varConexionLocal.Close()
            End If
            varConexionLocal.Open()
            Dim sql As String = String.Format("DELETE FROM GarantiaMantenimiento WHERE NumFactura = '{0}'", Me.GridView1.GetRowCellValue(Me.GridView1.FocusedRowHandle, "NumFactura"))
            Dim cmd As New SqlCommand(sql, varConexionLocal)
            cmd.ExecuteNonQuery()

            varConexionLocal.Close()

            MessageBox.Show(String.Format("Se ha eliminado correctamente la factura número {0} del sistema de garantías.", Me.GridView1.GetRowCellValue(Me.GridView1.FocusedRowHandle, "NumFactura")), varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class